package org.dtrd.modules.pushmessage.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.dtrd.modules.pushmessage.entity.po.DtrdRlPushMessage;
import org.dtrd.modules.pushmessage.mapper.DtrdRlPushMessageMapper;
import org.dtrd.modules.pushmessage.service.IDtrdRlPushMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.dtrd.modules.pushmessage.entity.bean.PushMessageDetail;
import org.dtrd.modules.pushmessage.entity.bean.PushMessageListInfo;
import org.dtrd.modules.pushmessage.entity.bean.PushMessageListRequest;
import org.dtrd.modules.pushmessage.entity.bean.PushMessagePageRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiao
 * @since 2022-08-08
 */
@DS("multi-datasource1")
@Service
public class DtrdRlPushMessageServiceImpl extends ServiceImpl<DtrdRlPushMessageMapper, DtrdRlPushMessage> implements IDtrdRlPushMessageService {

    private DtrdRlPushMessageMapper dtrdRlPushMessageMapper;

    @Override
    public List<PushMessageListInfo> getPushMessageList(PushMessageListRequest request) {
        LambdaQueryWrapper<DtrdRlPushMessage> queryWrapper = baseQueryWrapper()
                .eq(StringUtils.isNotBlank(request.getOrgCode()),
                        DtrdRlPushMessage::getOrgCode, request.getOrgCode());
        List<DtrdRlPushMessage> poList = list(queryWrapper);
        List<PushMessageListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(PushMessageListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public IPage<PushMessageListInfo> pagePushMessage(PushMessagePageRequest request) {
        LambdaQueryWrapper<DtrdRlPushMessage> queryWrapper = baseQueryWrapper();
        IPage<DtrdRlPushMessage> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = this.page(page, queryWrapper);
        return page.convert(PushMessageListInfo::new);
    }

    @Override
    public PushMessageDetail getPushMessage(Integer dataId) {
        DtrdRlPushMessage dtrdRlPushMessage = getById(dataId);
        return Optional.ofNullable(dtrdRlPushMessage)
                        .map(po -> new PushMessageDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdatePushMessage(PushMessageDetail dto) {
        DtrdRlPushMessage dtrdRlPushMessage = new DtrdRlPushMessage();
        if(dto.getDataId() == null){
            dtrdRlPushMessage = dtrdRlPushMessage.init();
        }
        dtrdRlPushMessage.parseFromDto(dto);
        return saveOrUpdate(dtrdRlPushMessage);
    }

    @Override
    public boolean removePushMessage(Integer dataId) {
        return removeById(dataId);
    }

    @Override
    public List<DtrdRlPushMessage> getPushMessageListByOrgCodeAndStatus(String orgCode, Boolean applyStatus) {
        LambdaQueryWrapper<DtrdRlPushMessage> wrapper = baseQueryWrapper()
                .eq(DtrdRlPushMessage::getOrgCode,orgCode)
                .eq(DtrdRlPushMessage::getApplyStatus, applyStatus);
        List<DtrdRlPushMessage> list = this.list(wrapper);
        return list;
    }

    private LambdaQueryWrapper<DtrdRlPushMessage> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdRlPushMessage.class)
                        .eq(DtrdRlPushMessage::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdRlPushMessage> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdRlPushMessage.class)
                        .eq(DtrdRlPushMessage::getIsDel, 0);
    }
}
